<?php

namespace app\apicompany\controller;

use app\apicompany\model\Education;
use app\apicompany\model\Intention;
use app\apicompany\model\Undergo;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 个人简历
 */
class PersonageUser extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\apicompany\model\PersonageUser();
        $this->modelIntention = new Intention();
        $this->modelundergo = new Undergo();
        $this->modelEducation = new Education();
        $this->modelcultivate = new \app\apicompany\model\Cultivate();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 个人投递意向
     * @return void
     */
    public function intention_list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $where = [];
            if (!empty($input['id'])) {
                $where['m.id'] = $input['id'];
            }
            if (!empty($input['keyWork'])) {
                $where['m.name'] = ['like', '%' . $input['keyWork'] . '%'];
            }
            if (!empty($input['mobile'])) {
                $where['u.mobile'] = $input['mobile'];
            }
            if (!empty($input['identity'])) {
                $where['u.identity'] = $input['identity'];
            }
            if (!empty($input['personage_post_id'])) {
                $where['m.personage_post_id'] = $input['personage_post_id'];
            }
            $order = 'm.createtime asc';
            $pageArr = array_merge($pageArr, $input);

            $data = $this->modelIntention->alias('m')
                ->join('personage_user u', 'm.user_id=u.user_id')
                ->join('personage_post p', 'p.id=m.personage_post_id')
                ->where($where)
                ->order($order)
                ->field('m.id,concat(m.province,m.city,m.district,m.address) as address,
                m.nature,m.status_name,u.username,u.gender,u.identity,u.birthDate,u.headimage,p.name,
                u.id as personage_user_id,u.user_id
                ')
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);

            if ($input['export'] == 1) {
                $this->export($data->items());
                die;
            }
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 列表
     * @return void
     */
    public function info()
    {
        $input = input();
        try {

            $where = [];
            if (!empty($input['id'])) {
                $where['id'] = $input['id'];
            }
            $order = 'createtime asc';
            $data = $this->model->alias('m')
                ->where($where)
                ->order($order)
                ->find();
            $data['intention'] = $this->modelIntention->alias('m')
                ->join('personage_post s','s.id=m.personage_post_id')
                ->field('m.*,s.name as personage_post_name')
                ->where('m.user_id', $data['user_id'])
                ->select();
            $data['undergo'] = $this->modelundergo->where('user_id', $data['user_id'])->select();
            $data['education'] = $this->modelEducation->where('user_id', $data['user_id'])->select();
            $data['cultivate'] = $this->modelcultivate->where('user_id', $data['user_id'])->select();
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    public function export($data)
    {
        header("Content-type:application/vnd.ms-excel");   //声明内容类型为excel
        header("Content-Disposition:attachment;filename=excel导出.xls");  //content-disposition设置attachment为弹窗下载，inline时会内嵌浏览器显示，当然对jpg等文件有效，excel文件不能内嵌，可自行翻阅文档了解；filename定义文件名称与扩展名
        echo "编号\t";
        echo "用户名\t";
//        echo "手机号\t";
        echo "地址\t";
        echo "出生日期\t";
        echo "性别\t";
        echo "职位\t";
        foreach ($data as $v) {
            echo "\n";
            echo $v['id'] . "\t";
            echo $v['username'] . "\t";
//            echo $v['mobile'] . "\t";
            echo $v['address'] . "\t";
            echo $v['birthDate'] . "\t";
            echo ($v['gender'] == '1' ? '男' : '女') . "\t";
            echo ($v['name'] ?? '') . "\t";
        }
    }
}